package cn.xiaoming.car.server.account.service.impl;

import cn.xiaoming.car.server.account.dao.persist.repository.IDeptRepository;
import cn.xiaoming.car.server.account.pojo.entity.Dept;
import cn.xiaoming.car.server.account.pojo.entity.User;
import cn.xiaoming.car.server.account.pojo.param.DeptAddNewParam;
import cn.xiaoming.car.server.account.pojo.vo.*;
import cn.xiaoming.car.server.account.service.IDeptService;
import cn.xiaoming.car.server.common.ex.ServiceException;
import cn.xiaoming.car.server.common.pojo.vo.PageData;
import cn.xiaoming.car.server.common.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 处理部门数据的业务实现类
 *
 * @author java@john.cn
 * @version 1.0
 */
@Slf4j
@Service
public class DeptServiceImpl implements IDeptService {
    @Value("${xiaoming-car.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private IDeptRepository deptRepository;

    public DeptServiceImpl() {
        log.debug("创建业务类对象：DeptServiceImpl");
    }

    @Override
    public void addNew(DeptAddNewParam deptAddNewParam) {
        // 检查部门是否被占用
        {
            String deptname = deptAddNewParam.getDeptName();
            int count=deptRepository.countByDeptName(deptname);
            if(count>0){
                String message = "添加部门失败，部门名已经被占用！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
            }
        }
        Dept dept=new Dept();
        BeanUtils.copyProperties(deptAddNewParam,dept);

        int rows = deptRepository.insert(dept);
        if (rows != 1) {
            String message = "添加管理员失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除标签】的业务，参数：{}", id);
        Object queryResult = deptRepository.getStandardById(id);
        if (queryResult == null) {
            String message = "删除标签失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        log.debug("即将执行删除数据，参数：{}", id);
        int rows = deptRepository.deleteById(id);
        if (rows != 1) {
            String message = "删除标签失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void setEnable(Long id) {
        updateEnableById(id, 1);
    }

    @Override
    public void setDisable(Long id) {
        updateEnableById(id, 0);
    }

    @Override
    public DeptStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询部门】业务，参数：{}", id);
        DeptStandardVO currentDept = deptRepository.getStandardById(id);
        if (currentDept == null) {
            String message = "获取部门详情失败，尝试访问的部门数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return currentDept;
    }

    @Override
    public PageData<DeptListItemVO> list(Integer pageNum) {
        log.debug("开始处理【查询用户列表】的业务，页码：{}", pageNum);
        PageData<DeptListItemVO> pageData = deptRepository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<DeptListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询用户列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageData<DeptListItemVO> pageData = deptRepository.list(pageNum, pageSize);
        return pageData;
    }

    private void updateEnableById(Long id, Integer enable) {
        log.debug("开始处理【{}标签】的业务，ID：{}，目标状态：{}", ENABLE_TEXT[enable], id, enable);
        DeptStandardVO queryResult = deptRepository.getStandardById(id);
        if (queryResult == null) {
            String message = ENABLE_TEXT[enable] + "标签失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        if (queryResult.getEnable().equals(enable)) {
            String message = ENABLE_TEXT[enable] + "标签失败，当前用户已经处于"
                    + ENABLE_TEXT[enable] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        Dept dept=new Dept();
        dept.setId(id);
        dept.setEnable(enable);
        log.debug("即将修改数据，参数：{}", dept);
        int rows = deptRepository.updateById(dept);
        if (rows != 1) {
            String message = ENABLE_TEXT[enable] + "标签失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }
}
